package com.clock.web.controller.shop;

import com.clock.common.core.domain.AjaxResult;
import com.clock.common.core.page.TableDataInfo;
import com.clock.common.enums.shop.EmployeeStatus;
import com.clock.shop.domain.EmployeeInfo;
import com.clock.shop.domain.ShopEmployee;
import com.clock.shop.domain.ShopShop;
import com.clock.shop.domain.vo.EmployeeVo;
import com.clock.shop.service.IShopEmployeeService;
import com.clock.shop.service.IShopShopService;
import com.clock.shop.service.IShopClockService;
import com.clock.shop.service.impl.ShopClockServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import static com.clock.common.utils.PageUtils.startPage;

import com.clock.common.core.controller.BaseController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/clock")
public class ShopClockController extends BaseController{

    @Autowired
    private IShopEmployeeService iShopEmployeeService;

    @Autowired
    private IShopShopService iShopShopService;

    @Autowired
    private IShopClockService iShopClockService ;

    //上完钟的员工id
    Long OverId;

    //被点单的员工id
    Long OId;

    //用链表装保存员工信息的对象
    LinkedList<EmployeeInfo> employeeInfos = new LinkedList<>();

    //排钟方式：0-轮钟，1-轮排，默认轮钟
    int clock = 0;

    //店员选择轮钟方式
    @PutMapping("/updateClock")
    public AjaxResult updateClock(@RequestParam("action") String action){
        if ("1".equals(action)) {
            clock = 1;
            return AjaxResult.success("已修改为轮排");
        } else if ("0".equals(action)) {
            return AjaxResult.success("已修改为轮钟");
        }
            return AjaxResult.error("修改失败");

    }

    //技师确认完成订单
    @PreAuthorize("@ss.hasPermi('shop:employee:update')")
    @PutMapping("/orderOver")
    public TableDataInfo orderOver(@RequestParam("action") String action, @RequestParam Long employeeId) {
        //验证请求参数
        if ("complete".equals(action)){
            //更新员工状态为空闲
            EmployeeVo employeeVo = new EmployeeVo();
            employeeVo = iShopEmployeeService.selectShopEmployeeByEmployeeId(employeeId);
            employeeVo.setEmployeeStatus("0");
            ShopEmployee shopEmployee = new ShopEmployee();
            BeanUtils.copyProperties(employeeVo, shopEmployee);
            iShopEmployeeService.updateShopEmployee(shopEmployee);
            OverId = employeeId;
            //根据排钟方式排序
            employeeInfos = iShopClockService.sortEmployee(OverId, clock);
            OverId = null;
            return getDataTable(employeeInfos);
        }else {
            return null;
        }

    }

    //员工被点单
    @PreAuthorize("@ss.hasPermi('shop:employee:update')")
    @PutMapping("/orderEmployee")
    public AjaxResult orderEmployee(@RequestParam("action") String action, @RequestParam Long employeeId) {
        //验证请求参数
        if ("order".equals(action)){
            //更新员工状态为服务中
            EmployeeVo employeeVo = new EmployeeVo();
            employeeVo = iShopEmployeeService.selectShopEmployeeByEmployeeId(employeeId);
            employeeVo.setEmployeeStatus("1");
            ShopEmployee shopEmployee = new ShopEmployee();
            BeanUtils.copyProperties(employeeVo, shopEmployee);
            iShopEmployeeService.updateShopEmployee(shopEmployee);
            OId = employeeId;
            //遍历链表，将员工状态改为进行中
            for(EmployeeInfo employee: employeeInfos) {
                if(shopEmployee.getEmployeeId().equals(employee.getEmoloyeeId())) {
                    employee.setEmployeeStatus(1);
                }
                break;
            }
            //将被点的店员id置为空
            OId = null;
            return AjaxResult.success("点单成功");
//            OId = shopEmployee.getEmployeeId();
//            ishopClockService.sortEmployee(OId);
//            return getDataTable(employeeInfos);
        }
            return AjaxResult.error("点单失败");


    }

    //店员更新员工顺序接口
    @PostMapping("/updateLine")
    public TableDataInfo updateLine(@RequestBody List<Long> EmployeeIds) {
//        List<Long> userIds;
        //更新排好序后的员工信息
        LinkedList<EmployeeInfo> employeeInfos = iShopClockService.updateLine(EmployeeIds);
        return getDataTable(employeeInfos);
    }

    // 查询员工列表
//    @PreAuthorize("@ss.hasPermi('shop:employee:list')")
//    @GetMapping("/employeeList")
//    public TableDataInfo list() {
//        // 获取当前用户
//        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
//        String currentUsername = authentication.getName();
//
//        // 根据账号获取店铺信息
//        ShopShop shopShop = iShopShopService.selectShopByEmployeeName(currentUsername);
////        if (shopShop == null) {
////            // 如果店铺不存在，返回空数据和0总数
////            return new TableDataInfo(new ArrayList<>(), 0, "店铺不存在");
////        }
//
//        // 查询该店的员工
//        List<ShopEmployee> employees = iShopEmployeeService.selectShopEmployeeListByShopId(shopShop.getShopId());
//
//        // 封装结果
//        List<EmployeeVo> employeeVos = new ArrayList<>();
//        for (ShopEmployee employee : employees) {
//            EmployeeVo employeeVo = new EmployeeVo();
//            BeanUtils.copyProperties(employee, employeeVo);
//            employeeVos.add(employeeVo);
//        }
//
//        // 返回封装好的员工数据
//        return getDataTable(employeeVos);
//    }



}


